package kiwi.assist.dev.guieditor.command {
	import kiwi.action.base.DispatchEventAction;
	import kiwi.action.base.WaitEventAction;
	import kiwi.action.queue.Queue;
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.debug.Log;
	import kiwi.event.ActionEvent;

	import org.robotlegs.mvcs.Command;

	import flash.filesystem.File;

	/**
	 * @author zhangming.luo
	 */
	public class BatchPublishCommand extends Command {
		[Inject]
		public var event : GUIEditorEvent;
		private var dir : File;
		private var cnt : int = 0;

		override public function execute() : void {
			var path : String = event.data;
			dir = File.applicationDirectory.resolvePath(path);
			var global : Queue = new Queue();
			for each (var file:File in dir.getDirectoryListing()) {
				if (file.extension == "ui") {
					var queue : Queue = new Queue();
					queue.submit(new DispatchEventAction(eventDispatcher, new GUIEditorEvent(GUIEditorEvent.LOAD_UIXML, file.nativePath)));
					queue.submit(new WaitEventAction(eventDispatcher, GUIEditorEvent.BUILD_COMPLETE));
					queue.submit(new DispatchEventAction(eventDispatcher, new GUIEditorEvent(GUIEditorEvent.PUBLISH_AS, true)));
					global.submit(queue);
				}
			}
			global.addEventListener(ActionEvent.ACTION_CHILD_COMPLETE, onChildComplete);
			global.addEventListener(ActionEvent.ACTION_COMPLETE, onComplete);
			global.execute();
		}

		private function onChildComplete(event : ActionEvent) : void {
			cnt++;
		}

		private function onComplete(event : ActionEvent) : void {
			Log.alert("全部结束 " + cnt);
		}
	}
}
